package com.tjian.fafa.user.module.courier.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tjian.fafa.user.R;
import com.tjian.fafa.user.module.bean.BaseBean;
import com.tjian.fafa.user.module.callback.ResultCallback;
import com.tjian.fafa.user.module.courier.adapter.UserDiaryAdapter;
import com.tjian.fafa.user.module.courier.bean.MoodInBean;
import com.tjian.fafa.user.module.courier.bean.MoodInfoBean;
import com.tjian.fafa.user.module.courier.bean.UserInfoBean;
import com.tjian.fafa.user.module.courier.constants.HttpConstants;
import com.tjian.fafa.user.module.courier.loadimage.AsynImageLoader;
import com.tjian.fafa.user.module.courier.util.HttpUtil;
import com.tjian.fafa.user.module.courier.view.TitleBar;
import com.tjian.fafa.user.module.listview.XListView;
import com.tjian.fafa.user.module.listview.XListView.IXListViewListener;

//import org.apache.http.NameValuePair;
//import org.apache.http.message.BasicNameValuePair;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.NameValuePair;
import cz.msebera.android.httpclient.message.BasicNameValuePair;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class UserInfoActivity extends BaseActivity implements
        IXListViewListener
{

    private String userName;
    private String userId;
    private TitleBar userInfoTitleBar;
    private UserInfoBean userInfoBean;
    private ImageView userLogo;
    private TextView tvUserName;
    private TextView tvUserAge;
    private TextView tvUserCity;
    private XListView myListView;
    private int page = 1;
    private int INIT_LOADING = 0;// 初始化第一次加载
    private int MORE_LOADING = 1;// 加载更多
    UserDiaryAdapter adapter;

    AsynImageLoader imageLoad = new AsynImageLoader();
    List<MoodInfoBean> allData = null;
    private View ListViewHeadView;

    @Override
    protected void onCreate(Bundle arg0)
    {

        super.onCreate(arg0);
        setContentView(R.layout.user_info);
        initData();
        initViews();
    }

    @Override
    public void initData()
    {

        super.initData();
        final String userId = getIntent().getStringExtra("userid");
        String userName = getIntent().getStringExtra("username");
        this.userName = userName;
        this.userId = userId;
        userInfoTitleBar = (TitleBar) findViewById(R.id.userInfoTitleBar);
        userInfoTitleBar.showLeftImageAndRightStr(userName, "写私信", getResources().getDrawable(R.drawable.app_back), new OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                finish();
            }
        }, new OnClickListener()
        {

            @Override
            public void onClick(View arg0)
            {
                Toast.makeText(UserInfoActivity.this, "写私信", Toast.LENGTH_LONG).show();
                Intent intent = new Intent();
                intent.setClass(UserInfoActivity.this, FriendsMessageActivity.class);
                intent.putExtra("userid", userId);
                startActivity(intent);
            }
        });
    }

    @Override
    public void initViews()
    {

        super.initViews();

        ListViewHeadView = LayoutInflater.from(UserInfoActivity.this).inflate(R.layout.listview_head, null);
        userLogo = (ImageView) findViewById(R.id.iv_leftlogo);
        tvUserName = (TextView) findViewById(R.id.tvName);
        tvUserAge = (TextView) findViewById(R.id.tvAge);
        tvUserCity = (TextView) findViewById(R.id.tvCity);
        myListView = (XListView) findViewById(R.id.allDiaryListView);
        myListView.setPullLoadEnable(true);
        myListView.setXListViewListener(this);

        myListView.addHeaderView(ListViewHeadView);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("id", userId));
        initProgressDialog();
        HttpUtil.doPost(HttpConstants.HTTP_GET_USERINFO, params,
                new ResultCallback()
                {

                    @Override
                    public void getReslt(String result)
                    {

                        BaseBean b = JSON.parseObject(result, BaseBean.class);
                        if (b.getRespcode().equals("0"))
                        {
                            final UserInfoBean userInfo = JSON.parseObject(
                                    b.getData(), UserInfoBean.class);
                            if (userInfo != null)
                            {
                                userInfoBean = userInfo;
                                imageLoad.showImageAsyn(
                                        userLogo,
                                        HttpConstants.HTTP_REQUEST
                                                + userInfo.getImage(),
                                        R.drawable.ic_launcher);
                                tvUserName.setText(userInfo.getName());
                                tvUserAge.setText(userInfo.getAge());
                                tvUserCity.setText(userInfo.getCity());

                                getListViewData(INIT_LOADING);
                                close();
                            }
                            close();
                        }
                        else
                        {
                            Toast.makeText(UserInfoActivity.this, "访问网络失败",
                                    Toast.LENGTH_LONG).show();
                            close();
                        }
                    }
                });

    }

    /**
     * 初始化listview 数据
     */
    public void getListViewData(final int code)
    {

        List<NameValuePair> getMeDiarysP = new ArrayList<NameValuePair>();
        getMeDiarysP.add(new BasicNameValuePair("page", String.valueOf(page)));
        getMeDiarysP.add(new BasicNameValuePair("userid", userId));
        initProgressDialog();
        HttpUtil.doPost(HttpConstants.HTTP_ALL_DIARY_FORUSERID, getMeDiarysP,
                new ResultCallback()
                {

                    @SuppressWarnings("null")
                    @Override
                    public void getReslt(String result)
                    {


                        if (code == INIT_LOADING)
                        {
                            BaseBean b = JSON.parseObject(result, BaseBean.class);
                            if (b.getRespcode().equals("0"))
                            {

                                Type type = new TypeToken<MoodInBean>()
                                {
                                }.getType();
                                // Gson解析
                                Gson gson = new Gson();

                                MoodInBean json = gson.fromJson(result, type);
                                allData = json.getData();
                                if (allData.size() > 0)
                                {
                                    adapter = new UserDiaryAdapter(
                                            UserInfoActivity.this, allData);
                                    myListView.setAdapter(adapter);
                                    page++;
                                }
                                else
                                {
                                    Toast.makeText(UserInfoActivity.this, "暂无相关信息", Toast.LENGTH_LONG).show();
                                }
                                close();
                            }
                            else
                            {
                                close();
                                Toast.makeText(UserInfoActivity.this, "服务器响应失败", Toast.LENGTH_LONG).show();
                            }
                            close();
                        }
                        else if (code == MORE_LOADING)
                        {
                            BaseBean b = JSON.parseObject(result, BaseBean.class);
                            if (b.getRespcode().equals("0"))
                            {

                                Type type = new TypeToken<MoodInBean>()
                                {
                                }.getType();
                                // Gson解析
                                Gson gson = new Gson();

                                MoodInBean json = gson.fromJson(result, type);
                                List<MoodInfoBean> allDatas = json.getData();
                                if (allDatas.size() > 0)
                                {
                                    for (int i = 0; i < allDatas.size(); i++)
                                    {
                                        MoodInfoBean mb = allDatas.get(i);
                                        allData.add(mb);
                                    }
                                    adapter.notifyDataSetChanged();
                                    page++;
                                    onLoad();
                                }
                                else
                                {
                                    Toast.makeText(UserInfoActivity.this, "暂无相关信息", Toast.LENGTH_LONG).show();
                                    onLoad();
                                }
                                close();
                            }
                            else
                            {
                                close();
                                Toast.makeText(UserInfoActivity.this, "服务器响应失败", Toast.LENGTH_LONG).show();
                                onLoad();
                            }
                        }

                    }
                });

    }

    @Override
    public void onRefresh()
    {

        myListView.stopRefresh();
    }

    @Override
    public void onLoadMore()
    {

        getListViewData(MORE_LOADING);

    }

    /**
     * 关闭进度
     */
    private void onLoad()
    {
        myListView.stopRefresh();
        myListView.stopLoadMore();
        myListView.setRefreshTime("刚刚");
    }

}
